package com.woniuxy.bookmall.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.bookmall.mapper.TBookMapper;
import com.woniuxy.bookmall.utils.RedisUtils;
import com.woniuxy.bookmall.vo.EsBook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {

    @Autowired
    TBookMapper bookMapper ;

    @Autowired
    RedisUtils redisUtils ;

    /**
     * @param i    0 ，热卖  1  新书  2  收藏
     * @param count  查询几条
     * @return
     */
    public List<EsBook> findEsBookByType(int i ,int count){
        String item = "findEsBookByType"+i+count ;
        if (i == 0) {
            List<EsBook> lists = (List<EsBook>)redisUtils.hget("BOOk_OF_TYPE",item);
            if (lists != null) {
                return lists ;
            }else {
                synchronized (BookService.class) {
                    lists = (List<EsBook>)redisUtils.hget("BOOk_OF_TYPE",item);
                    if (lists != null){
                        return lists ;
                    }else {
                        lists = bookMapper.selectHotSailBook(count);
                        redisUtils.hset("BOOk_OF_TYPE",item,lists,30*60);
                        return  lists ;
                    }
                }
            }
        }else if (i == 1) {
            List<EsBook> lists = (List<EsBook>)redisUtils.hget("BOOk_OF_TYPE",item);
            if (lists != null) {
                return lists ;
            }else {
                synchronized (BookService.class) {
                    lists = (List<EsBook>)redisUtils.hget("BOOk_OF_TYPE",item);
                    if (lists != null){
                        return lists ;
                    }else {
                        lists = bookMapper.selectNewBook(count);
                        redisUtils.hset("BOOk_OF_TYPE",item,lists,30*60);
                        return  lists ;
                    }
                }
            }
        }else if (i == 2) {
            List<EsBook> lists = (List<EsBook>)redisUtils.hget("BOOk_OF_TYPE",item);
            if (lists != null) {
                return lists ;
            }else {
                synchronized (BookService.class) {
                    lists = (List<EsBook>)redisUtils.hget("BOOk_OF_TYPE",item);
                    if (lists != null){
                        return lists ;
                    }else {
                        lists = bookMapper.selectCollectionBook(count);
                        redisUtils.hset("BOOk_OF_TYPE",item,lists,30*60);
                        return  lists ;
                    }
                }
            }
        }
        return null;
    }

    public PageInfo<EsBook> findEsBookByBookType(int id,String bname,int now,int size) {
        String item = this.getClass().getName()+"findEsBookByBookType"+id+bname+now+size ;
        PageInfo<EsBook> pageInfo = (PageInfo<EsBook>)redisUtils.hget("BOOK_LIST",item);
        if (pageInfo != null) {
            return pageInfo ;
        }else {
            synchronized (BookService.class) {
                pageInfo = (PageInfo<EsBook>)redisUtils.hget("BOOK_LIST",item);
                if (pageInfo != null) {
                    return  pageInfo ;
                }else {
                    PageHelper.startPage(now,size);
                    List<EsBook> lists = bookMapper.selectBookByTypeId(id,bname);
                    pageInfo = new PageInfo<>(lists) ;
                    redisUtils.hset("BOOK_LIST",item,pageInfo,15*60);
                    return pageInfo ;
                }
            }
        }
    }

    public EsBook findEsBookById(Long id) {
        //1.查询
        //2. 更新点击量
        EsBook esBook = (EsBook) redisUtils.get("BOOK_SINGLTON"+id);
        if (esBook != null) {
            return esBook ;
        }else {
            synchronized (BookService.class) {
                esBook = (EsBook) redisUtils.get("BOOK_SINGLTON"+id);
                if (esBook != null) {
                    return esBook ;
                }else {
                    esBook = bookMapper.selectEsBookById(id);
                    redisUtils.set("BOOK_SINGLTON"+id,esBook,5*60);
                    return esBook ;
                }
            }
        }
    }
}
